<template>
  <div class="app-container">
    <el-form :model="caseParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="甲方识别码" prop="ah">
        <el-input
          v-model="caseParams.nbajbh"
          placeholder="请输入甲方识别码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="委托方批次">
        <el-select v-model="caseParams.zfid" placeholder="请选择委托方" clearable @change="getBatch">
          <el-option
            v-for="item in zfOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-select v-model="caseParams.batchId" placeholder="请选择批次" clearable @change="handleQuery">
          <el-option
            v-for="item in pcOptions"
            :key="item.id"
            :label="item.batchName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="诉讼主体">
        <el-select v-model="caseParams.ssztid" placeholder="请选择诉讼主体" clearable @change="handleQuery">
          <el-option
            v-for="item in ssztOptions"
            :key="item.id"
            :label="item.dwmc"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="被告" prop="ajmc">
        <el-input
          v-model="caseParams.ajmc"
          placeholder="请输入被告名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审查状态" prop="zt">
        <el-select v-model="queryParams.zt" placeholder="请选择立案状态" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.fs_hkjh_sh"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fs:schedule:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange">
      <el-table-column label="甲方识别码" align="center" prop="nbajbh">
        <template slot-scope="scope">
          <span>{{ scope.row.schedule.fsCase.nbajbh }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资产包" align="center" prop="ajmc">
        <template slot-scope="scope">
          <span>{{ scope.row.schedule.fsCase.zfName }}</span>
          &nbsp;&nbsp;
          <span>{{ scope.row.schedule.fsCase.batchName }}</span>
          <br>
          <span>{{ scope.row.schedule.fsCase.ssztmc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="被告" align="center" width="250px">
        <template slot-scope="scope">
          <span>{{ scope.row.schedule.fsCase.bgDsr.xm }}</span>
          &nbsp;&nbsp;
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.schedule.fsCase.bgDsr.xb"/>
          &nbsp;&nbsp;
          <span>{{ scope.row.schedule.fsCase.bgDsr.sjhm }}</span>
          <br>
          <span>{{ scope.row.schedule.fsCase.bgDsr.zjhm }}</span>
          <br>
          <span>{{ scope.row.schedule.fsCase.bgDsr.dz }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划" align="center" prop="jhrq" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.schedule.jhje }}</span>
          <br>
          <span>{{ parseTime(scope.row.schedule.jhrq, '{y}-{m}-{d}') }}</span>
          <br>
          <span class="cellBtn">
            <el-button title="查看 还款计划"
                       size="mini"
                       type="text"
                       icon="el-icon-time"
                       @click="handleViewHkjh(scope.row)"
                       v-hasPermi="['fs:schedule:edit']"
            >
            </el-button>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="zt">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fs_hkjh_sh" :value="scope.row.zt"/>
        </template>
      </el-table-column>
      <el-table-column label="实还" align="center" prop="shrq" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.shje }}</span>
          <br>
          <span>{{ parseTime(scope.row.shrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件" align="center" prop="shrq" width="180">
        <template slot-scope="scope">
          <template v-for="(item,index) in scope.row.files">
            <el-link :key="index" type="primary" :href="item.oosUrl" target="_blank">{{ item.name }}</el-link>
            <br>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="归属" align="center" width="120">
        <template slot-scope="scope">
          <span>律师： {{ scope.row.schedule.fsCase.owner }}</span>
          <br>
          <span>调解员： {{ scope.row.schedule.fsCase.tjy }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核" align="center" prop="shrq" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.scrMc }}</span>
          <br>
          <span>{{ parseTime(scope.row.scsj, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width"
                       min-width="100px">
        <template slot-scope="scope">
          <el-button v-if="scope.row.zt==0"
                     size="mini"
                     type="text"
                     icon="el-icon-finished"
                     @click="handleSh(scope.row)"
                     v-hasPermi="['fs:schedule:edit']"
          >通过
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 查看还款计划 -->
    <el-dialog :title="hkxqTitle" :visible.sync="hkxqOpen" width="800px" append-to-body>
      <el-form ref="hkform" :model="hkform" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="还款金额" prop="hkje">
              {{ hkform.hkje }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="期数" prop="qs">
              {{ hkform.qs }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="首次还款日" prop="schkr">
              {{ hkform.schkr }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table class="hktable" :data="hkjhList">
        <el-table-column label="序号" align="center" prop="xh"/>
        <el-table-column label="计划日期" align="center" prop="jhrq">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.jhrq, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="计划金额" align="center" prop="jhje"/>
        <el-table-column label="备注" align="center" prop="remark" width="200px"/>
        <el-table-column label="状态" align="center" prop="zt">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.fs_hkjh_zt" :value="scope.row.zt"/>
          </template>
        </el-table-column>
        <el-table-column label="还款信息" align="center" prop="shrq" width="200px">
          <template slot-scope="scope">
            <template v-for="(item,index) in scope.row.pays">
              <span>{{ item.shje }}元</span>&nbsp;
              <span>{{ parseTime(item.shrq, '{y}-{m}-{d}') }}</span>&nbsp;
              <dict-tag :options="dict.type.fs_hkjh_sh" :value="item.zt"/>&nbsp;
              <br>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hkxqcancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPay, getPay, delPay, addPay, updatePay, shPay } from "@/api/fs/pay";
import {Div, Add, Sub, Mul, formatDate, formatNumber, toChineseNumber} from "@/api/fs/common";
import {getRepaymentByCaseId, listBatch, listSszt, listZf} from "@/api/fs/case";

export default {
  name: "Schedule",
  dicts: ['fs_hkjh_zt', 'fs_hkjh_sh', 'fs_hkjh_hklx', 'fs_hkjh_year', 'fs_hkjh_month', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 还款计划表格数据
      scheduleList: [],
      hkjhList:[],
      zfOptions: [],
      pcOptions: [],
      ssztOptions: [],
      // 弹出层标题
      title: "",
      hktitle: "",
      hkxqTitle: "",
      // 是否显示弹出层
      open: false,
      hkopen: false,
      hkxqOpen: false,
      statistic: {
        id: null,
        year: null,
        month: null,
        ssr: null,
        znum: 0,
        zje: 0,
        dhnum: 0,
        dhje: 0,
        dshnum: 0,
        dshje: 0,
        yhnum: 0,
        yhje: 0,
        yqnum: 0,
        yqje: 0
      },
      caseParams: {
        pageNum: 1,
        pageSize: 10,
        ah: null,
        yg: null,
        bg: null,
        ajlx: null,
        ajmc: null,
        fyid: null,
        sqbdje: null,
        sqlaay: null,
        zt: null,
        cjsj: null,
        gxsj: null,
        ownerId: null,
        ajzt: null,
        tjyId: null,
        sfid: null,
        sid: null,
        qxid: null,
        zfid: null,
        batchId: null,
        nbajbh: null,
        ssztid: null,
        lafs: null,
        ifPost: null,
        ifRepayment: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scheduleId: null,
        repaymentId: null,
        zt: 0,
        shrq: null,
        shje: null,
        ssr: null,
        scr: null,
        scsj: null,
        params: null,
      },
      // 表单参数
      form: {},
      hkform: {},
      // 表单校验
      rules: {}
    };
  },
  activated() {
    this.getList();
  },
  created() {
    this.getList();
    this.getZf();
    this.getSszt();
  },
  mounted() {
  },
  methods: {
    getZf() {
      listZf().then(response => {
        this.zfOptions = response.data;
      });
    },
    getBatch() {
      this.caseParams.batchId = null;
      this.pcOptions = [];
      this.handleQuery();
      if (this.caseParams.zfid && this.caseParams.zfid > 0) {
        listBatch(this.caseParams.zfid).then(response => {
          this.pcOptions = response.data;
        });
      }
    },
    getSszt() {
      listSszt().then(response => {
        this.ssztOptions = response.data;
      });
    },
    /** 查询还款计划列表 */
    getList() {
      this.loading = true;
      let params = {};
      if (this.caseParams.nbajbh) {
        params['nbajbh'] = this.caseParams.nbajbh;
      }
      if (this.caseParams.ajmc) {
        params['ajmc'] = this.caseParams.ajmc;
      }
      if (this.caseParams.zfid) {
        params['zfid'] = this.caseParams.zfid;
      }
      if (this.caseParams.batchId) {
        params['batchId'] = this.caseParams.batchId;
      }
      if (this.caseParams.ssztid) {
        params['ssztid'] = this.caseParams.ssztid;
      }
      this.queryParams.params = params;
      listPay(this.queryParams).then(response => {
        let list = response.rows;
        for (let i = 0; i < list.length; i++) {
          let item = list[i];
          let shje = formatNumber(item.shje.toFixed(2));
          item.shje = shje;
        }
        this.scheduleList = list;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        scheduleId: null,
        repaymentId: null,
        zt: null,
        shrq: null,
        shje: null,
        scr: null,
        scsj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    handleSh(row) {
      const id = row.id;
      this.$modal.confirm('确认该还款已通过审核吗？').then(function () {
        return shPay(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("修改成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      let params = {};
      if (this.caseParams.nbajbh) {
        params['nbajbh'] = this.caseParams.nbajbh;
      }
      if (this.caseParams.ajmc) {
        params['ajmc'] = this.caseParams.ajmc;
      }
      if (this.caseParams.zfid) {
        params['zfid'] = this.caseParams.zfid;
      }
      if (this.caseParams.batchId) {
        params['batchId'] = this.caseParams.batchId;
      }
      if (this.caseParams.ssztid) {
        params['ssztid'] = this.caseParams.ssztid;
      }
      this.queryParams.params = params;
      let fileName = "还款计划审核"
      var dt = new Date();
      let year = dt.getFullYear();
      let month = (dt.getMonth() + 1).toString().padStart(2, '0');
      let date = dt.getDate().toString().padStart(2, '0');
      fileName += "-" + year + "/" + month + "/" + date + ".xlsx";
      this.download('fs/pay/export', {
        ...this.queryParams
      }, fileName)
    },
    handleViewHkjh(row) {
      getRepaymentByCaseId(row.schedule.caseId).then(response => {
        let data = response.data;
        this.hkform = data;
        let hkjhList = data.hkjhList;
        for (let i = 0; i < hkjhList.length; i++) {
          hkjhList[i].jhje = formatNumber(hkjhList[i].jhje.toFixed(2));
          hkjhList[i].shje = formatNumber(hkjhList[i].shje.toFixed(2));
        }
        this.hkjhList = hkjhList;
        this.hkxqOpen = true;
        this.hkxqTitle = "还款计划详情";
      });

    },
    hkxqcancel() {
      this.hkxqOpen = false;
    },
  }
};
</script>
<style>

.el-table__cell .cell div {
  display: inline;
}

.cell .el-link--inner {
  font-size: 12px;
}

.cellBtn button {
  font-size: 20px !important;
  padding: 0 !important;
}
</style>
